[C#] 同網站下,不同應用程式在ASP.NET MVC 使用 Owin 驗證的登入登出問題


Posted by mike-hsieh on 2023-12-28

當您使用 ASP.NET MVC 4.6.2 和 Owin 驗證,並在同一 IIS 站點下運行多個相同應用程式的實例時,您可能會遇到 "多個實例下登入/登出問題"(Login/logout issue for multiple IIS same applications under the same site)。這個問題通常是由於應用程式間的 Cookie 管理不當或者配置相同造成的。

解決方案:

1. 設定唯一的 Cookie 名稱:

確保每個應用程式實例使用唯一的 Cookie 名稱。如果多個應用共享同一個 Cookie 名稱,將會導致驗證資料相互干擾。

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    CookieName = "YourUniqueAppNameCookie", // 確保是唯一的
    // 其他設定...
});

2. 設定machineKey:

 2.1 設定自動產生的Key
  <machineKey 
    validationKey="AutoGenerate,IsolateApps"
    decryptionKey="AutoGenerate,IsolateApps"
    validation="HMACSHA256"
    decryption="Auto"
    />
 2.2 設定實體key
<machineKey validation="HMACSHA256" decryption="AES" validationKey="65B9952371FF7CFAF6E2223715FF2CC2DECA91FAC9E4E9AEE68F435246065509" decryptionKey="17824DF15D1EBA25A212F3F240CB133B3D14C8F99261BBBBE95683C4B47DFADD" />

可以查詢 "asp.net machine key generator",會有一些網站自動產生這個。
也可以參考以下幾個網站:

  1. https://chaos.aspnet.cz/
  2. http://www.blackbeltcoder.com/Resources/MachineKey.aspx
  3. https://codewithshadman.com/machine-key-generator/

#CookieName #machinekey #iis #login #logout







Related Posts

What is Azure Release Pipeline

What is Azure Release Pipeline

Object get keys and values method

Object get keys and values method

程式導師實驗計畫 Week8 作業與解答

程式導師實驗計畫 Week8 作業與解答


Comments